/*
 * AltaDetalleRemito.java
 *
 * Created on 14 de marzo de 2007, 10:30
 */

package vista;

import controlador.Libro;
import java.util.LinkedList;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author  mfrias
 */
public class AltaDetalleRemito extends javax.swing.JFrame {
    
    private NuevoRemito padre;
    /** Creates new form AltaDetalleRemito */
    public AltaDetalleRemito(NuevoRemito nr) {
        initComponents();
        this.padre = nr;
        precio.setText("$ " + String.valueOf(new Libro((String)comboLibro.getSelectedItem()).getPrecio()));
        this.setTitle("Nuevo Detalle de remito");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        comboLibro = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cantidad = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        precio = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        comboLibro.setModel(getModel());
        comboLibro.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboLibroItemStateChanged(evt);
            }
        });
        comboLibro.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                comboLibroPropertyChange(evt);
            }
        });

        jLabel1.setText("Libro:");

        jLabel2.setText("Cantidad:");

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Agregar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("Precio:");

        precio.setText("--");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel3)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 14, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(comboLibro, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(precio, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(18, 18, 18)
                        .add(jButton2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1))
                    .add(cantidad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(comboLibro, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(precio, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(cantidad, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(17, 17, 17)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1)
                    .add(jButton2))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboLibroItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboLibroItemStateChanged
        precio.setText("$ " + String.valueOf(new Libro((String)comboLibro.getSelectedItem()).getPrecio()));
        cantidad.setText("");
    }//GEN-LAST:event_comboLibroItemStateChanged

    private void comboLibroPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_comboLibroPropertyChange
        
    }//GEN-LAST:event_comboLibroPropertyChange

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if(((String)comboLibro.getSelectedItem()).compareTo("") == 0)
            JOptionPane.showMessageDialog(this, "Debe seleccionar un titulo", "Error", JOptionPane.ERROR_MESSAGE);
        else if(cantidad.getText().compareTo("") == 0)
            JOptionPane.showMessageDialog(this, "Debe ingresar una cantidad", "Error", JOptionPane.ERROR_MESSAGE);
        else {
            Libro libro = new Libro((String)this.comboLibro.getSelectedItem()); //libro por titulo
            try{
                this.padre.insertarFila(libro, Integer.parseInt(cantidad.getText()));
                this.padre.setLabelPrecio();
            }catch(NumberFormatException e){
                JOptionPane.showMessageDialog(this, "El formato de cantidad debe ser un numero entero.", "Error", JOptionPane.ERROR_MESSAGE);
                return;
            }
            this.dispose();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    public ComboBoxModel getModel(){
        LinkedList lista = Libro.getLibros();
        String[] titulos = new String[lista.size()];
        for(int i = 0; i < lista.size() ; i++){
            titulos[i] = new String(((Libro)lista.get(i)).getTitulo());
        }
        cbm = new DefaultComboBoxModel(titulos);
        return cbm;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField cantidad;
    private javax.swing.JComboBox comboLibro;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel precio;
    // End of variables declaration//GEN-END:variables
    private DefaultComboBoxModel cbm;
}
